package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.action.impl.AbstractActionFactory;
import com.cloudlead.commonData.service.BaseOilCardService;
import com.cloudlead.commonData.vo.BaseCompany;
import com.cloudlead.commonData.vo.BaseOilCard;
import com.cloudlead.fleetAction.service.BaseOilCardEditSer;
import com.cloudlead.fleetService.vo.BaseOilCardVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jinnoo on 2015/8/12.
 */
@Service
public class  BaseOilCardSerImpl implements BaseOilCardEditSer {

    @Autowired
    BaseOilCardService baseOilCardService;

    @Override
    public void createOildCard(BaseOilCardVo vo) {
        BaseOilCard boc = new BaseOilCard();
        BeanUtils.copyProperties(vo, boc);
        if(vo.getBaseCompany()!=null){
            BaseCompany bc = new BaseCompany();
            bc.setId(vo.getBaseCompany());
            boc.setBaseCompany(bc);
        }
        boc.setFlag("0");
        baseOilCardService.create(boc);

    }

    @Override
    public void editOildCard(BaseOilCardVo vo) {

        BaseOilCard boc =  baseOilCardService.view(vo.getId());

        if(vo.getBaseCompany()!=null&&!"".equals(vo.getBaseCompany())){
            BaseCompany bc = new BaseCompany();
            bc.setId(vo.getBaseCompany());
            boc.setBaseCompany(bc);
        }
        if(vo.getAccountNumber()!=null&&!"".equals(vo.getAccountNumber()))//油卡编号
            boc.setAccountNumber(vo.getAccountNumber());

        if (vo.getUser()!=null&&!"".equals(vo.getUser()))//经办人
            boc.setUser(vo.getUser());

        if(vo.getNumber()!=null&&!"".equals(vo.getNumber()))//主卡编号
            boc.setNumber(vo.getNumber());

        if(vo.getComment()!=null&&!"".equals(vo.getComment()))//备注
            boc.setComment(vo.getComment());

        if(vo.getStatus()!=null&&!"".equals(vo.getStatus()))//状态
            boc.setStatus(vo.getStatus());

        baseOilCardService.update(boc);
    }

    @Override
    public BaseOilCardVo findOneOilCard(BaseOilCardVo vo) {
        BaseOilCard boc =new BaseOilCard();
        BeanUtils.copyProperties(vo, boc);
        BaseOilCard boc1=baseOilCardService.findOne(boc);
        BaseOilCardVo bocv=new BaseOilCardVo();
        BeanUtils.copyProperties(boc1, bocv);
        return bocv;
    }

    @Override
    public Page<BaseOilCardVo> findAllOilCard(BaseOilCardVo vo,int page,int rows) {
        BaseOilCardVo vo1 = new BaseOilCardVo();
        if(vo!=null) vo1=vo;
        PageRequest pr = new PageRequest();
        List<Term> tr=new ArrayList<Term>();
        //查询条件
        tr.add(new Term("flag","0"));//未删除

        if (vo.getAccountNumber()!=null&&!"".equals(vo.getAccountNumber()))//油卡编号
            tr.add(new Term("accountNumber",Operator.LK,vo.getAccountNumber()));

        if (vo.getType()!=null&&!"".equals(vo.getType()))//油卡类型
            tr.add(new Term("type",vo.getType()));

        if (vo.getNumber()!=null&&!"".equals(vo.getNumber()))//主卡编号
            tr.add(new Term("number",vo.getNumber()));

        if (vo.getStatus()!=null&&!"".equals(vo.getStatus()))//油卡状态
            tr.add(new Term("status",vo.getStatus()));

        if(vo.getStartDate()!=null&&!"".equals(vo.getStartDate()))//购卡日期
            tr.add(new Term("date",Operator.GE, vo.getStartDate()));

        if(vo.getDestDate()!=null&&!"".equals(vo.getDestDate()))//购卡日期
            tr.add(new Term("date",Operator.LE, vo.getDestDate()));

        Filter filter=new Filter( new Criteria( tr));
        pr.setSize(rows);
        pr.setPage(page);
            pr.setFilter(filter);
        Page<BaseOilCard> pb=(Page<BaseOilCard>)baseOilCardService.findAll(pr);
        Page<BaseOilCardVo> pb1=new Page<BaseOilCardVo>();
        BeanUtils.copyProperties(pb, pb1);
        return pb1;
    }

    @Override
    public void delOilCard(String id) {
        BaseOilCard boc = baseOilCardService.view(id);
        if(boc!=null) {
            boc.setFlag("1");
            baseOilCardService.update(boc);
        }
    }
}
